#!/bin/sh
# Commit message regex, contains project code, so customize it!
COMMIT_MSG_REGEX="(.*)"

# Misc variables
BOLD=$(tput bold)
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
NO_STYLE=$(tput sgr0)

echo "${BOLD}Verifying commit message${NO_STYLE}"
# Get commit message
commit_msg=$(cat "$1")
# Allow us to read user input below, assigns stdin to keyboard
exec < /dev/tty
# If the commit message does not match the regex, do stuff
if echo "$commit_msg" | grep -qE "$COMMIT_MSG_REGEX"; then
  echo "${GREEN}The commit message is OK!${NO_STYLE}"
else
  printf "%s" "${YELLOW}The commit message does not contain an issue code, are you sure you want to commit this?${NO_STYLE} (y/n): "
  read -r yn
  if [ "$yn" = "" ]; then
    yn='Y'
  fi
  case $yn in
      [Yy] ) echo "${YELLOW}OK, commiting anyway${NO_STYLE}";;
      * ) echo "${RED}You didn't add a issue code, aborting commit...${NO_STYLE}"; exit 1;;
  esac
fi

exec <&-